import axios from "axios";

const axiosInstance = axios.create({
    // baseURL: '/api/',
    baseURL: 'http://localhost:3000/',
    timeout: 3000
});

export async function register(username: string, password: string) {
    return await axiosInstance.post('/user/register', {
        username, password
    });
}

export async function login(username: string, password: string) {
    return await axiosInstance.post('/user/login', {
        username, password
    });
}
export async function list(name?: string) {
    return await axiosInstance.get('/book/list',{
        params: {
            ...(name && { name })
        }
    });
}

export async function getBookById(id: number) {
    return await axiosInstance.get(`/book/${id}`);
}

// 创建新图书
export async function createBook(bookData: {
    name: string;
    author: string;
    description: string;
    cover?: string;
}) {
    return await axiosInstance.post('/book/create', bookData);
}

// 更新图书信息
export async function updateBook(bookData: {
    id: number;
    name: string;
    author: string;
    description: string;
    cover?: string;
}) {
    return await axiosInstance.put('/book/update', bookData);
}

// 删除图书
export async function deleteBook(id: number) {
    return await axiosInstance.delete(`/book/delete/${id}`);
}

export async function uploadImage(file: File) {
    const formData = new FormData();
    formData.append('file', file);

    return await axiosInstance.post('/book/upload', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    });
}